package utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

public class JedisUtils {

    private static String host = "";
    private static int port = 0;
    private static int maxTotal = 0;
    private static int maxIdle = 0;
    private static JedisPool jedisPool = null;

    static {
        //是专门用来读取src下的properties配置文件的。
        ResourceBundle rb = ResourceBundle.getBundle("jedis");
        host = rb.getString("host");
        port = Integer.parseInt(rb.getString("port"));
        maxTotal = Integer.parseInt(rb.getString("maxTotal"));
        maxIdle = Integer.parseInt(rb.getString("maxIdle"));
        //创建配置信息对象
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        //创建连接池
        jedisPool = new JedisPool(config,host,port);

    }

    public static Jedis getJedis(){
        return  jedisPool.getResource();
    }

    public static void release(Jedis jedis){
        jedis.close();
    }

    public static void main(String[] args) {
        //获取连接
        Jedis jedis = getJedis();
        String aaa = jedis.get("aaa");
        System.out.println(aaa);
        //释放资源
        release(jedis);
    }
}
